package com.lilosoft.business.cga.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lilosoft.business.cga.dao.CgaCaseMapper;
import com.lilosoft.business.cga.models.CgaCase;
import com.lilosoft.business.cga.models.CgaCaseExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 小峰
 * Date: 2019-08-29
 * Time: 11:07
 */
@Service
public class CgaCaseService {
    @Autowired
    private CgaCaseMapper cgaCaseMapper;
    @Value("${program.parameter.pagesize}")
    private Integer pageSize;

    public void updateIsPrecityById(CgaCase cgaCase) {
        cgaCase.setIsPrecity(1);
        cgaCaseMapper.updateByPrimaryKeySelective(cgaCase);
    }

    public PageInfo<CgaCase> selectByExample(CgaCaseExample example, Integer pageNumber) {
        PageHelper.startPage(pageNumber, pageSize);
        List<CgaCase> cgaCases = cgaCaseMapper.selectByExample(example);
        return new PageInfo<>(cgaCases);
    }

    /**
     * 根据主键Id, 批量更新同步到市里的状态
     *
     * @param primaryKeys
     * @return
     */
    public int updateIsPrecityByPrimaryKeyList(List<String> primaryKeys) {
        if (primaryKeys != null && primaryKeys.size() > 0) {
            return cgaCaseMapper.updateIsPrecityByPrimaryKeyList(primaryKeys);
        }
        return 0;
    }

    /**
     * 更新同步市里为Null,以更下次同步
     * @param caseId
     * @return
     */
    public int updateIsPrecityToNull(String caseId){
       return cgaCaseMapper.updateIsPrecityToNull(caseId);
    }

    public CgaCase selectOneByCaseId(String caseId){
        CgaCaseExample example=new CgaCaseExample();
        CgaCaseExample.Criteria criteria = example.createCriteria();
        criteria.andCaseIdEqualTo(caseId);
        List<CgaCase> cgaCases = cgaCaseMapper.selectByExample(example);
        if(null !=cgaCases && cgaCases.size()>0){
            return cgaCases.get(0);
        }
        return null;
    }
}
